import 'package:flutter/material.dart';

class PPConditionChoiceItem extends StatefulWidget {

    PPConditionChoiceItem({ Key key, this.data, this.itemWidth = 80, this.isSelect, this.index, this.action }): super(key: key);

    final Map data;
    final double itemWidth;
    final bool isSelect;
	final int index;
	final Function action;

    _PPConditionChoiceItemState createState() => _PPConditionChoiceItemState();
}

class _PPConditionChoiceItemState extends State<PPConditionChoiceItem> {
    
    Map data;
    double itemWidth;
    int index;
    
    @override
    void initState() {
        data = widget.data;
        itemWidth = widget.itemWidth;
        index = widget.index;
        super.initState();
    }

	@override
	Widget build(BuildContext context) {
		String title = data['title'];
		bool isSelect = data['isSelect'];
		return (FlatButton(
                padding: EdgeInsets.all(0),
				child: Container(
                    width: itemWidth,
                    margin: EdgeInsets.all(0),
                    padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
                    decoration: _getBoxDecoration(isSelect),
					child: Text(title,
                    maxLines: 1,
						style: _getTextStyle(isSelect),
						textAlign: TextAlign.center),
				    ),					
				onPressed: _onPressButtonActon
            ));
	}

	_getBoxDecoration(isSelect) {
		if(isSelect) {
			return BoxDecoration(color: Color.fromRGBO(67,201,182,0.1), border: new Border.all(color: Color.fromRGBO(67,201,182,1), width: 1), borderRadius: BorderRadius.circular(3));
		} else {
			return BoxDecoration(color: Colors.white30, border: new Border.all(color: Color.fromRGBO(24,25,25,0.33), width: 1), borderRadius: BorderRadius.circular(3));
		}
	}

	_getTextStyle(isSelect) {
		if(isSelect) {
			return TextStyle(fontSize: 14, color: Colors.black);
		} else {
			return TextStyle(fontSize: 14, color: Colors.black);
		}
	}

	_onPressButtonActon() {
		if(widget.action != null) {
			widget.action(data, index);
		}
		print(index);
	}

}